﻿using System;
using Furion;
using Furion.Schedule;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SyncVideo.Application.Jobs;
using SyncVideo.Core;

namespace SyncVideo.Web.Core
{
    public class Startup : AppStartup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddConsoleFormatter();
            services.AddJwt<JwtHandler>();

            services.AddCorsAccessor();
            services.AddSchedule(options =>
            {
                options.AddJob<SyVideo>(concurrent: false, Triggers.Secondly()); // 串行，每秒执行
            });
            services.AddFileLogging("_logs/{0:yyyy}-{0:MM}/{0:yyyy}-{0:MM}-{0:dd}.log", options =>
            {
                options.MinimumLevel = LogLevel.Information;

                options.DateFormat = "yyyy-MM-dd HH:mm:ss.fff";

                options.FileNameRule = fileName =>
                {
                    return string.Format(fileName, DateTime.Now);
                };
            });
            services.AddControllers()
                    .AddInjectWithUnifyResult();
            services.AddHttpRemote();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseCorsAccessor();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseInject(string.Empty);

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            //var list1= DbContext.Instance.GetConnection("caiji").Queryable<MediaEntity>().ToList();
            //var list2 = DbContext.Instance.GetConnection("shujv").Queryable<MediaEntity>().ToList();
        }
    }
}
